package com.hdax.dm.service;

import com.hdax.dm.dto.item.IndexHotItemDto;
import com.hdax.dm.dto.item.IndexNavDto;
import com.hdax.dm.entity.base.Image;
import com.hdax.dm.entity.item.Item;
import com.hdax.dm.entity.item.ItemType;
import com.hdax.dm.feign.base.ImageFeign;
import com.hdax.dm.feign.item.ItemFeign;
import com.hdax.dm.feign.item.ItemTypeFeign;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IndexNavService {

    @Autowired
    private ItemTypeFeign itemTypeFeign;
    @Autowired
    private ItemFeign itemFeign;
    @Autowired
    private ImageFeign imageFeign;

    public List<IndexNavDto> levelOne(){
        //使用openfeign实现对提供者的远程调用
        List<ItemType> levelOneList = itemTypeFeign.types(0L);
        List<IndexNavDto> navDtos = new ArrayList<>();
        levelOneList.forEach(levelOne->{
            IndexNavDto dto = new IndexNavDto();
            BeanUtils.copyProperties(levelOne,dto);
            List<Item> hotItems = itemFeign.items(levelOne.getId());
            List<IndexHotItemDto> hotItemDtos = new ArrayList<>();
            hotItems.forEach(hotItem->{
                IndexHotItemDto hotItemDto = new IndexHotItemDto();
                Image image = imageFeign.image(hotItem.getId(), 2L, 1L);
                hotItemDto.setImgUrl(image == null?"":image.getImgUrl());
                BeanUtils.copyProperties(hotItem,hotItemDto);
                hotItemDtos.add(hotItemDto);
            });
            dto.setHotItems(hotItemDtos);
            navDtos.add(dto);
        });
        return navDtos;
    }

    public List<IndexNavDto> searchFenLei(Long id){
        List<ItemType> itemTypeList = itemTypeFeign.types(id);
        List<IndexNavDto> indexNavDtos = new ArrayList<>();
        itemTypeList.forEach(itemType -> {
            IndexNavDto indexNavDto = new IndexNavDto();
            BeanUtils.copyProperties(itemType,indexNavDto);
            indexNavDtos.add(indexNavDto);
        });
        return indexNavDtos;
    }


}
